package com.heima.wemedia.controller.v1;

import com.heima.apis.wemedia.WmNewsControllerApi;
import com.heima.model.common.constants.WemediaConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController  implements WmNewsControllerApi {

    //注入 service 层 对象
    @Autowired
    private WmNewsService wmNewsService;

    @PostMapping("/list")
    @Override
    public ResponseResult findAll(@RequestBody  WmNewsPageReqDto wmNewsPageReqDto) {
        return wmNewsService.findAll(wmNewsPageReqDto);
    }

    /**
     * 文章提交：草稿、保存、修改
     * @param dto
     * @return
     */
    @PostMapping("/submit")
    @Override
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {
        // 保存
        if (dto.getStatus().equals(WmNews.Status.SUBMIT.getCode())) {
            return wmNewsService.saveNews(dto, WmNews.Status.SUBMIT.getCode());
        }else {  // 草稿
            return wmNewsService.saveNews(dto, WmNews.Status.NORMAL.getCode());
        }
    }


    /**
     * 根据id 查询 文章
     * @param id
     * @return
     */
    @GetMapping("/one/{id}")
    @Override
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return  wmNewsService.findWmNewsById(id);
    }

    /**
     * 删除文章
     * @return
     */
    @DeleteMapping("/del_news/{id}")
    @Override
    public ResponseResult delNews(@PathVariable("id") Integer id) {
        return wmNewsService.delNews(id);
    }


    /**
     * 文章1 上下架
     * @return
     */
    @Override
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }


//======================================1/17
    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    @GetMapping("/findOne/{id}")
    @Override
    public WmNews findByid(@PathVariable("id") Integer id) {
        //根据id 查询文章
        return wmNewsService.getById(id);
    }

    /**
     * 修改文章
     * @param wmNews
     * @return
     */
    @PutMapping("/update")
    @Override
    public ResponseResult updateWmNews(@RequestBody  WmNews wmNews) {
        wmNewsService.updateById(wmNews);
       return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //===========================1/18
    /**
     * 查询需要发布的文章id列表
     * @return
     */
    @GetMapping("/findRelease")
    @Override
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

    /**
     * 查询文章列表(作者名称)
     *
     * @param dto
     * @return
     */
    @PostMapping("/list_vo")
    @Override
    public ResponseResult findList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findList(dto);
    }
//===============================1/20

    /**
     * 审核通过文章
     * @param dto
     * @return
     */
    @PostMapping("/auth_pass")
    @Override
    public ResponseResult authPass(@RequestBody  NewsAuthDto dto) {
        return wmNewsService.updateStatus(WemediaConstants.WM_NEWS_AUTH_PASS,dto);
    }

    /**
     * 审核驳回文章
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody  NewsAuthDto dto) {
        return wmNewsService.updateStatus(WemediaConstants.WM_NEWS_AUTH_FAIL,dto);
    }

    /**
     * 查询文章详情
     * @param id
     * @return
     */
    @GetMapping("/one_vo/{id}")
    @Override
    public ResponseResult findWmNewsVo(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }
}
